package com.demo.security.access.audit;

import java.util.Collection;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.security.core.Authentication;

import com.demo.security.access.AccessLogicInvocation;
import com.demo.security.access.LogicAttribute;
import com.demo.security.access.audit.prepost.PreAuditAttribute;
import com.demo.security.access.audit.prepost.PrePostInvocationAuditAdvice;

/**
 * 访问审计调用
 * 
 * @author ryuu.kk
 *
 */
public class AccessAuditInvocation implements AccessLogicInvocation<MethodInvocation> {

	private PrePostInvocationAuditAdvice advice;
	@Override
	public boolean supports(Class<?> clazz) {
		return MethodInvocation.class.isAssignableFrom(clazz);
	}

	@Override
	public boolean supports(LogicAttribute attribute) {
		return attribute instanceof PreAuditAttribute;
	}

	@Override
	public void invoke(Authentication authentication, MethodInvocation mi, Collection<LogicAttribute> attributes) {
		for (LogicAttribute attr : attributes) {
			if (supports(attr)) {
				advice.before(authentication, mi, (PreAuditAttribute) attr);
			}
		}
	}

	public void setAdvice(PrePostInvocationAuditAdvice advice) {
		this.advice = advice;
	}
}
